package com.hns.phone.pushMsg.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.master.entity.SysAppPhoneSetInfoEntity;
import com.hns.frame.service.CommonService;
import com.hns.phone.entity.SysAppCanPhoneRelEntity;
import com.hns.phone.pushMsg.vo.PhoneMsgParamsVo;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * app推送信息service(包括设置，更改用户token，tag相关通知下发)
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class PhoneMsgService extends CommonService {
	
	@Autowired
	private FillCodeDscValue fillCodeDscValue;

	/**
	 * 获取用户所拥有的tag
	 * @param params
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
//	@SuppressWarnings("unchecked")
//	public List<PhoneMsgVo> getTagByUser(PhoneMsgParamsVo params) {
//		List<PhoneMsgVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneMsgSetting.getTagByUser", params), PhoneMsgVo.class);
//		return list;
//	}

	/**
	 * 更新用户token
	 * @param params
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Transactional
	public void updateUserToken(PhoneMsgParamsVo params,SysUserEntity user) {
		SysAppCanPhoneRelEntity sysAppCanPhoneRelEntity = null;
		HashMap map = new HashMap();
		map.put("phoneType", params.getPhoneType());
		map.put("userId", user.getId());
		this.sqlMapDao.delete("phoneMsgSetting.deleteUserToken", map);
		sysAppCanPhoneRelEntity = new SysAppCanPhoneRelEntity();
		sysAppCanPhoneRelEntity.setRelId(UtilString.getUUID());
		sysAppCanPhoneRelEntity.setPhoneType(params.getPhoneType());
		sysAppCanPhoneRelEntity.setCorpId(user.getCorpId());
		sysAppCanPhoneRelEntity.setUserId(user.getId());
		sysAppCanPhoneRelEntity.setTokenId(params.getTokenId());
		sysAppCanPhoneRelEntity.setUpdateTime(new Date());
		sysAppCanPhoneRelEntity.setUpdateUser(user.getId());
//		sysAppCanPhoneRelEntity.setMsgToken(params.getTokenId());
//		this.commonEntityDao.save(bizMsgUserTokenEntity);
		this.sqlMapDao.insert("phoneMsgSetting.insertUserToken", sysAppCanPhoneRelEntity);
	}

	/**
	 * 根据用户id获取推送设置信息
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SysAppPhoneSetInfoEntity getPushMsgSetting(SysUserEntity user) {
		HashMap map = new HashMap();
		map.put("userId", user.getId());
		List<SysAppPhoneSetInfoEntity> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneMsgSetting.getPushMsgSetting", map), SysAppPhoneSetInfoEntity.class);
		SysAppPhoneSetInfoEntity entity = null;
		if(PubUtil.isEmpty(list)){
    		//新注册用户 数据库加入默认配置信息，返回给app
			entity = saveDefaultMsgSetting(user);
    	}else{
    		entity = list.get(0);
    	}
    	return entity;
	}
	
	/**
	 * 添加用户对应默认设置
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional
    private SysAppPhoneSetInfoEntity saveDefaultMsgSetting(SysUserEntity user){
    	HashMap params = new HashMap();
    	params.put("setId", UtilString.getUUID());
    	params.put("corpId", user.getCorpId());
    	params.put("userId", user.getId());
    	params.put("code", 0);
    	params.put("notice", "");
    	sqlMapDao.insert("phoneMsgSetting.initDefaultSetting", params);
    	List<SysAppPhoneSetInfoEntity> result = PubUtil.convertListMapToObject(sqlMapDao.selectList("phoneMsgSetting.getPushMsgSetting", user.getId()), SysAppPhoneSetInfoEntity.class);
    	return result.get(0);
    }

	/**
	 * 设置报警信息
	 * @param entity
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional
	public String updatePushMsgSetting(SysAppPhoneSetInfoEntity entity) {
		String msgType = "0";
		HashMap param = new HashMap();
		param.put("userId", entity.getUserId());
		this.sqlMapDao.delete("phoneMsgSetting.delSettingByuid", param);
		commonEntityDao.save(entity);
		msgType = "1";
    	return msgType;
	}
	
}
